package com.globallogic.strelchuk.usermanager.bean;

import org.springframework.context.annotation.Scope;

import java.util.Date;
import java.util.List;

import com.globallogic.strelchuk.usermanager.domain.Activity;
import com.globallogic.strelchuk.usermanager.service.ActivityService;

import java.io.Serializable;

import javax.inject.Inject;
import javax.inject.Named;

@Named
@Scope("session")
public class ActivityBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	private ActivityService activityService;

	@Inject
	private UserBean userBean;

	@SuppressWarnings("unused")
	private List<Activity> listAllActivities;

	public ActivityBean() {
		currentActivity = new Activity();
	}

	private Integer id;
	private String name;
	private Date createDate;
	private Date startDate;
	private Date dueDate;
	private int done;
	private boolean editable;
	private Integer userId;
	private Activity currentActivity;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getDueDate() {
		return dueDate;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	public int getDone() {
		return done;
	}

	public void setDone(int done) {
		this.done = done;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public List<Activity> getListAllActivities(Integer userId) {
		return activityService.listAllActivities(userId);
	}

	public void setListAllActivities(List<Activity> listAllActivities) {
		this.listAllActivities = listAllActivities;
	}

	public ActivityService getActivityService() {
		return activityService;
	}

	public void setActivityService(ActivityService activityService) {
		this.activityService = activityService;
	}

	public Activity getCurrentActivity() {
		return currentActivity;
	}

	public void setCurrentActivity(Activity currentActivity) {
		this.currentActivity = currentActivity;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public String saveAction(Activity activity) {
		activityService.editActivity(activity, activity.getId());
		return "activities";
	}

	public String editAction(Activity activity) {
		setCurrentActivity(activity);
		return "editactivity";
	}

	public String addAction() {
		Activity act = new Activity();
		act.setName(getName());
		act.setStartDate(getStartDate());
		act.setDueDate(getDueDate());
		act.setDone(0);
		act.setUserId(userBean.getCurrentUser().getId());

		activityService.addActivity(act);
		clearForm();

		return null;
	}

	public String deleteAction(Integer id) {
		activityService.removeActivity(id);
		return null;
	}

	public String markActivityDone(Integer id) {
		activityService.markActivityDone(id);
		return null;
	}

	public String markActivityUndone(Integer id) {
		activityService.markActivityUndone(id);
		return null;
	}

	private void clearForm() {
		setName("");
		setStartDate(null);
		setDueDate(null);
		setDone(0);
	}
}